#include "UdpServer.hpp"
#include "Log.hpp"
#include <memory>
#include <cstdio>
#include <vector>



void Usage(std::string proc)
{
    std::cout<<"\n\rUsage: "<<proc<<"port[1024+]\n"<<std::endl;
}

bool SafeCheck(const std::string& cmd)
{
    int safe = false;
    std::vector<std::string> key_word = {
        "rm",
        "mv",
        "cp",
        "kill",
        "sudo",
        "unlink",
        "uninstall",
        "yum",
        "top"
    };

    for(auto & word:key_word)
    {
        auto pos = cmd.find(word);
        if(pos!=std::string::npos)
            return false;
    }
    return true;
}

std::string Handler(const std::string&str,const std::string& clientip,uint16_t clientport)
{
    std::string res = "server get a message: ";
    res+=str;

    //std::cout<<res<<" ip: "<<clientip<<" port: "<<clientport<<std::endl;
    std::cout<<"["<<clientip<<":"<<clientport<<"] "<<res<<std::endl;
    return res;
}

std::string ExcuteCommand(const std::string & cmd)
{
    std::cout<<"get a request cmd:"<<cmd<<std::endl;
    if(!SafeCheck(cmd))
        return "Bad man";
    FILE *fp = popen(cmd.c_str(),"r");
    if(nullptr ==fp)
    {
        perror("open");
        return "error";
    }
    std::string result;
    char buffer[4096];
    while(true)
    {
        char *ok = fgets(buffer,sizeof(buffer),fp);
        if(ok==nullptr)
            break;
        result+=buffer;
    }
    pclose(fp);

    return result;
}
//   ./udpserve port
int main(int argc,char *argv[])
{
    if(argc!=2)
    {
        Usage(argv[0]);
        exit(0);
    }
    uint16_t port = std::stoi(argv[1]);
    std::unique_ptr<UdpServer> svr(new UdpServer(port));

    svr->Init();
    svr->Run(Handler);
    return 0;
}



